// 平台管理模块路由处理函数

// 引入jwt
const jwt = require('jsonwebtoken')
const secretKey = 'ethink'    // 密钥
const utils = require("../common/utils")
const server = require("../server/home")

// 添加角色
exports.addRole = (req, res) => {
  const data = req.body
  server.addRole(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除角色
exports.deleteRole = (req, res) => {
  let id = req.query.id
  server.deleteRole(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改角色
exports.updateRole = (req, res) => {
  const data = req.body
  server.updateRole(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取角色列表
exports.getRoleList = (req, res) => {
  let pageSize = req.query.pageSize || 20
  let pageNum = req.query.pageNum
  let role_name = req.query.role_name.trim()
  server.getRoleList(pageNum, pageSize, role_name).then(result => {
    return res.send({
      code: 200,
      message: '获取角色列表成功',
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取角色菜单
exports.getRolePermission = (req, res) => {
  let role_id = req.query.id
  server.getRolePermission(role_id).then(result => {
    return res.send({
      code: 200,
      message: "获取角色菜单成功",
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 配置角色权限（菜单）
exports.updateRolePermission = (req, res) => {  
  let role_id = req.body.id
  const menu_ids = req.body.menu_ids
  server.updateRolePermission(role_id, menu_ids).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取所有菜单
exports.getAllMenu = (req, res) => {
  server.getAllMenu().then(result => {
    return res.send({
      code: 200,
      message: "获取成功",
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加菜单
exports.addMenu = (req, res) => {
  const data = req.body
  server.addMenu(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改菜单
exports.updateMenu = (req, res) => {
  const data = req.body
  server.updateMenu(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除菜单
exports.deleteMenu = (req, res) => {
  let id = req.query.id
  let token = req.get('Authorization').split(' ')[1]
  // token解析
  const data = jwt.verify(token, secretKey)
  server.deleteMenu(id, data.role_id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加用户
exports.addUser = (req, res) => {
  const data = req.body
  server.addUser(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除用户
exports.deleteUser = (req, res) => {
  let id = req.query.id
  server.deleteUser(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改用户
exports.updateUser = (req, res) => {
  const data = req.body
  server.updateUser(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 重置用户密码
exports.resetPassword = (req, res) => {
  let id = req.params.id
  server.resetPassword(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取用户列表
exports.getUserList = (req, res) => {
  const data = req.query
  server.getUserList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取用户列表成功",
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加产品分类
exports.addCategory = (req, res) => {
  const data = req.body
  server.addCategory(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除产品分类
exports.deleteCategory = (req, res) => {
  let id = req.query.id
  server.deleteCategory(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改产品分类
exports.updateCategory = (req, res) => {
  const data = req.body
  server.updateCategory(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取产品分类
exports.getCategory = (req, res) => {
  server.getCategory().then(result => {
   return res.send({
     code: 200,
     message: "获取产品分类列表成功",
     data: result
   })
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加产品
exports.addProduct = (req, res) => {
  const data = req.body
  server.addProduct(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除产品
exports.deleteProduct = (req, res) => {
  let id = req.query.id
  server.deleteProduct(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 批量删除产品
exports.batchDeleteProduct = (req, res) => {
  let data = req.body
  server.batchDeleteProduct(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改产品
exports.updateProduct = (req, res) => {
  const data = req.body
  server.updateProduct(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 产品上下架
exports.upDown = (req, res) => {
  let id = req.params.id
  let operation = req.params.operation
  server.upDown(id, operation).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 产品供应商
exports.getProductSupplier = (req, res) => {
  server.getProductSupplier().then(result => {
    return res.send({
      code: 200,
      message: "获取供应商列表成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取产品列表
exports.getProductList = (req, res) => {
  let data = req.query
  server.getProductList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取产品列表成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取供应商列表
exports.getSupplierList = (req, res) => {
  let data = req.query
  server.getSupplierList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取供应商列表成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加供应商
exports.addSupplier = (req, res) => {
  let data = req.body
  server.addSupplier(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改供应商
exports.updateSupplier = (req, res) => {
  let data = req.body
  server.updateSupplier(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除供应商
exports.deleteSupplier = (req, res) => {
  let id = req.query.id
  server.deleteSupplier(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取供应情况
exports.getCase = (req, res) => {
  let data = req.query
  server.getCase(data).then(result => {
    return res.send({
      code: 200,
      message: "获取供应情况成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加农场
exports.addLand = (req, res) => {
  let data = req.body
  server.addLand(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取农场列表
exports.getLandList = (req, res) => {
  let data = req.query
  server.getLandList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取农场列表成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改农场
exports.updateLand = (req, res) => {
  let data = req.body
  server.updateLand(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除农场
exports.deleteLand = (req, res) => {
  let id = req.query.id
  server.deleteLand(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加种植
exports.addPlant = (req, res) => {
  let data = req.body
  server.addPlant(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取种植列表
exports.getPlantList = (req, res) => {
  let data = req.query
  server.getPlantList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取种植列表成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 收获种植
exports.yieldPlant = (req, res) => {
  let data = req.query
  server.yieldPlant(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加养殖
exports.addAnimal = (req, res) => {
  let data = req.body
  server.addAnimal(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取养殖
exports.getAnimalList = (req, res) => {
  let data = req.query
  server.getAnimalList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取养殖列表成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加设备
exports.addDevice = (req, res) => {
  let data = req.body
  server.addDevice(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取设备列表
exports.getDeviceList = (req, res) => {
  let data = req.query
  server.getDeviceList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取设备列表成功",
      data: result 
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改设备
exports.updateDevice = (req, res) => {
  let data = req.body
  server.updateDevice(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除设备
exports.deleteDevice = (req, res) => {
  let id = req.query.id
  server.deleteDevice(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}